package com.datacvg.parsejson.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.datacvg.parsejson.entity.BiSysDimensionRes;
import com.datacvg.parsejson.mapper.BiSysDimensionResDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
public class InitController {

    @Autowired
    private BiSysDimensionResDao biSysDimensionResDao;

    public static Logger logger = LoggerFactory.getLogger(InitController.class);


    /**
     * @description: 初始化json
     * @param: []
     * @return: java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     * @author shentongshun
     * @date: 2021/9/13
     */
    @GetMapping("/init")
    public String init() throws IOException {
        //数据读取
        String json = getFileJson();
        JSONObject jsonObject = (JSONObject) JSON.parse(json);
        JSONArray orefArray = jsonObject.getJSONArray("O_REF");

        //计算添加了多少条
        int inser = 0;
        BiSysDimensionRes biSysDimensionRes = new BiSysDimensionRes();
        for (Object oref : orefArray) {

            JSONObject orefJson = (JSONObject) oref;
            String orgId = orefJson.getString("ORG_ID");
            String orgName = orefJson.getString("ORG_NAME");
            String orgParentid = orefJson.getString("ORG_PARENTID");
            String orgParentname = orefJson.getString("ORG_PARENTNAME");
            Integer orgLevl = orefJson.getInteger("ORG_LEVL");
            String horgId = orefJson.getString("HORG_ID");
            String horgParentid = orefJson.getString("HORG_PARENTID");


            biSysDimensionRes.setDResId(orgId);
            biSysDimensionRes.setDResClname(orgName);
            biSysDimensionRes.setDResParentid(orgParentid);
            biSysDimensionRes.setCreateTime("1");
            biSysDimensionRes.setResLevel(orgLevl);
            biSysDimensionRes.setCreateUser("sts");
            biSysDimensionRes.setDResRootid("9900");
            biSysDimensionResDao.insert(biSysDimensionRes);

            inser++;
            logger.info("影响条数：" + inser);

        }
        logger.info("添加条数：" + inser);
        return "添加条数：" + inser;


    }

    /**
     * @description: 注意：打成jar包很有可能读取不到数据。解决方案：修改 Json 文件的读取方式，代码如下：
     * @param: []
     * @return: java.lang.String
     * @author shentongshun
     * @date: 2021/9/13
     */

    public static String getFileJson() throws IOException {
        ClassPathResource classPathResource = new ClassPathResource("static/pufa.json");
        byte[] bytes = FileCopyUtils.copyToByteArray(classPathResource.getInputStream());
        return new String(bytes);
    }


}